package com.hyt.it.ogt.ksmonitor.statistics.enums;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 朱振宇
 * @since 2022-12-15
 */
public class MonitorStatisticsUtils {

    public static Map<Integer, String> convertToMap() {
        Map<Integer, String> mapResult = new HashMap<>();
        Arrays.stream(MonitorStatisticsTypeCode.values()).forEach(globalExceptionEnum -> mapResult.put(globalExceptionEnum.getCode(), globalExceptionEnum.getMsg()));
        return mapResult;
    }

    /**
     * @param clazz
     * @return
     * @Description 真正的工具类 - 将枚举转为Map使用，只要参数 clazz 继承了IbaseEnum
     * @author ChenChen
     * @date 2022/1/5
     */
    public static Map<String, String> generateConvertToMap(Class<? extends IBaseEnum> clazz) {
        Map<String, String> mapResult = new HashMap<>();

        if (!clazz.isEnum()) {
            throw new UnsupportedOperationException("参数不合法：非枚举类，不支持转换，请检查程序是否有误！");
        }

        // 通过class.getEnumConstants();获取所有的枚举字段和值
        IBaseEnum[] iBaseEnums = clazz.getEnumConstants();
        for (IBaseEnum iBaseEnum : iBaseEnums) {
            mapResult.put(iBaseEnum.getCode(), iBaseEnum.getMessage());
        }
        return mapResult;

    }
}
